package org.as4game.common.math
{
	import flash.geom.Vector3D;

	/**
	 * 四元组
	 */
	public class Quaternion 
	{
		public var w:Number;
		public var x:Number;
		public var y:Number;
		public var z:Number;
		
		public function Quaternion(w:Number=0, x:Number=0, y:Number=0, z:Number=0):void{
			this.w = w;
			this.x = x;
			this.y = y;
			this.z = z;
		} 
		
		public function clone():Quaternion{
			return new Quaternion(w, x, y, z);
		}
		
		public function cloneTo(out:Quaternion):void{
			out.w = w;
			out.x = x;
			out.y = y;
			out.z = z;
		}
		
		/**
		 * Returns the magnitude of the quaternion object.
		 */
		public function get magnitude():Number
		{
			return(Math.sqrt(w*w + x*x + y*y + z*z));
		}
		
		/**
		 * Normalises the quaternion object.
		 */
		public function normalize(val:Number = 1):void
		{
			var mag:Number = magnitude*val;
			
			x /= mag;
			y /= mag;
			z /= mag;
			w /= mag;
		}
		
		/**
		 * Fills the quaternion object with values representing the given rotation around a vector.
		 * 
		 * @param	x		The x value of the rotation vector.
		 * @param	y		The y value of the rotation vector.
		 * @param	z		The z value of the rotation vector.
		 * @param	angle	The angle in radians of the rotation.
		 */
		public function axis2quaternion(x:Number, y:Number, z:Number, angle:Number):void
		{
			var sin_a:Number = Math.sin(angle / 2);
			var cos_a:Number = Math.cos(angle / 2);
			
			this.x = x * sin_a;
			this.y = y * sin_a;
			this.z = z * sin_a;
			this.w = cos_a;
			normalize();
		}
	}
}